package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.ReturnAdapter;
import com.luck.easyfoodbatch.mvp.mine.RefundListModel;
import com.luck.easyfoodbatch.mvp.mine.RefundListPresenter;
import com.luck.easyfoodbatch.mvp.mine.RefundListView;
import com.luck.easyfoodbatch.mvp.model.RefundM;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.EasyRecyclerView;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReturnActivity extends BaseActivity implements RefundListView, SwipeRefreshLayout.OnRefreshListener, RecyclerArrayAdapter.OnLoadMoreListener {

    @BindView(R.id.recycler_view)
    EasyRecyclerView recyclerView;
    private ReturnAdapter adapter;
    private List<RefundM.DataBean.ModelsBean> list = new ArrayList<>();
    private RefundListModel refundListModel;
    private int totalPage = 1;
    private int page = 1;
    private int isRefresh = 0;// 0普通请求，1下拉刷新，2上拉加载

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_return, "退换货", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        refundListModel = new RefundListPresenter(this, mContext);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 3), getResources().getColor(R.color.color_f7)));
        recyclerView.setLayoutManager(manager);
        adapter = new ReturnAdapter(mContext);
//        adapter.setMore(R.layout.view_more, this);
//        adapter.setNoMore(R.layout.view_nomore);
        recyclerView.setAdapterWithProgress(adapter);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                RefundM.DataBean.ModelsBean dataBean = list.get(position);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("rid", dataBean.getRid() + "");
                intent.setClass(mContext, ReturnDetailsActivity.class);
                intent.putExtras(bundle);
                startActivityForResult(intent, 200);
            }
        });

        adapter.addAll(list);
        recyclerView.setRefreshListener(this);
        recyclerView.setAdapter(adapter);
        showProgressDialog(getString(R.string.app_loading));
        refundListModel.requestRefundList(isRefresh);
    }


    @Override
    public void refundListSuccess(RefundM model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                RefundM.DataBean data = model.getData();
                totalPage = model.getData().getTotalPage();
                // 下拉刷新 清空集合
                switch (isRefresh) {
                    case 0:
                        list.clear();
                        list = model.getData().getModels();
                        if (list == null) {
                            list = new ArrayList<>();
                        }
                        break;
                    case 1:
                        // 下拉刷新
                        list.addAll(model.getData().getModels());
                        if (list == null) {
                            list = new ArrayList<>();
                        }
                        break;
                }
                adapter.clear();
                adapter.addAll(list);
                adapter.notifyDataSetChanged();
            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 200:
                    showProgressDialog(getString(R.string.app_loading));
                    refundListModel.requestRefundList(isRefresh);
                    break;
            }
        }
    }

    @Override
    public void onLoadMore() {
        //刷新
        isRefresh = 1;
        page++;
        if (!isNetworkConnected()) {
            adapter.pauseMore();
            return;
        }
        if (page > totalPage) {
            adapter.stopMore();
            return;
        }
        refundListModel.requestRefundList(isRefresh);
    }

    @Override
    public void onRefresh() {
        isRefresh = 0;
        page = 1;
        refundListModel.requestRefundList(isRefresh);
    }
}
